package com.itheima.health.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.health.resource.entity.QueryPageBean;
import com.itheima.health.resource.pub.RedisConstant;
import com.itheima.health.service.MemberService;
import com.itheima.health.service.SetmealService;
import com.itheima.mapper.MemberMapper;
import com.itheima.mapper.SetmealMapper;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Member;
import com.itheima.pojo.Setmeal;
import com.sun.jersey.core.util.StringIgnoreCaseKeyComparator;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
@Transactional
public class MemberSrviceImpl extends ServiceImpl<MemberMapper, Member> implements MemberService {


    @Override
    public Member findByTelephone(String telephone) {
        Member member = baseMapper.findByTelephone(telephone);
        return member;
    }

    @Override
    public Map getMemberCount() {
        //从数据库中找出的数据封装到map集合中，返回给controller层
        //先把map对象创建出来
        Map map = new HashMap();

        //创建日期对象
        Calendar calendar = Calendar.getInstance();
        //我们数据库存的是2019年的，所以要向前推12个月
        calendar.add(Calendar.MONTH,-12);

        //把存放日期和存放会员数量的集合创建出来
        List<String> yearAndMonth = new ArrayList<>();
        List<Integer> memberCounts = new ArrayList<>();

        //遍历所有的月份，查出来每个月会员的会员总数
        for (int i = 0; i < 12; i++) {
            //获取日期
            Date time = calendar.getTime();
            //转换日期格式，
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
            String format = simpleDateFormat.format(time);
            //向list集合中存储查到并转换好的日期
            yearAndMonth.add(format);

            //字符串拼接，把日期格式拼接好，开始是2019-08-01到2019-08-31
            String bigDate = format+"-1";
            String endDate = format+"-31";
            //然后从数据库中查一个月的1号到31号所有的会员数量
            int count = baseMapper.findMemberCount(bigDate,endDate);

            memberCounts.add(count);

            //每遍历一次，就会少一个月
            calendar.add(Calendar.MONTH,1);
        }


        //yearAndMonth    memberCounts这两个值要和前端的数据一样，才能赋予他们
        map.put("yearAndMonth",yearAndMonth);
        map.put("memberCounts",memberCounts);

        //从数据库中找出的数据封装到map集合中，返回给controller层
        return map;
    }
}
